<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>使用jsencrypt执行OpenSSL的RSA加密，解密</title>
    </head>
    <!--引入jsencrypt.js-->
    <script src="https://cdn.bootcss.com/jsencrypt/3.0.0-beta.1/jsencrypt.js"></script>
    <script type="text/javascript">
        //公钥
        var PUBLIC_KEY = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvI7wuZpeTI6VnGjgRMFE\n' +
            'I/CGhEYbUIuFQDE3KZxqTy2eENd170PQzbg/iiMSXHcn8Kszm+MvOXTSjySd+Hvs\n' +
            'HMDFnIT7t0jiH3faF3Jr0txDk3OJe1TfRyGdnSsWDkU9cCLSi9qmgiV1s70SZLEm\n' +
            '2S2G32pLOvqR8BEKptmYyTZSqhsKhvC3VbRPO+WQZgdoHB6lDicsJPQjAZddDrNa\n' +
            'WnipV4sG9SEJ+hNGttkY29IdQv1zK7bfYDgcJG/XO0yzknukyX5EKiFh2RPjymze\n' +
            'qePEptMFmJe+Qighw+ttv5U+f6UXvHn7JSrUWQDN9dyjimUMYxWx06ZgfG1NJx9+\n' +
            'xQIDAQAB'
        PUBLIC_KEY = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyKCUeajaYjDjbpy6P9Xv\
hWlNNPdJIXewuz5+AW4FQC9mB8+x72Oaq3o9vkyE3iKDCeheA7rBCubBgw8p58FG\
66akjk0XqsrtZTN2ZWbgfkRSxEijxuFeEpLcKq1pr/AKt+GvIaACs9QdnyiqQASW\
WsM3DTEQsDgA2M0h3QU21lTh6v1fL1454CoyvJ+8PcNhTGq3pLsEJOMCBfb1N/Yq\
lZJwv0uh69fKkV/Q6LizoNKByHqjHcRMxxCepSPf8PzG2SbQqwe/D9DLNz3NrPI7\
uA200LayukqPUfKrQcyt0SFLeZ8mP1gLICfRXWY83bmBdr41yEkLdwwNVZPHb7X6\
1wIDAQAB'
        //var PUBLIC_KEY = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8HMr2CBpoZPm3t9tCVlrKtTmI4jNJc7/HhxjIEiDjC8czP4PV+44LjXvLYcSV0fwi6nE4LH2c5PBPEnPfqp0g8TZeX+bYGvd70cXee9d8wHgBqi4k0J0X33c0ZnW7JruftPyvJo9OelYSofBXQTcwI+3uIl/YvrgQRv6A5mW01QIDAQAB'
        //私钥
        var PRIVATE_KEY = 'MIIEowIBAAKCAQEAyKCUeajaYjDjbpy6P9XvhWlNNPdJIXewuz5+AW4FQC9mB8+x\n' +
            '72Oaq3o9vkyE3iKDCeheA7rBCubBgw8p58FG66akjk0XqsrtZTN2ZWbgfkRSxEij\n' +
            'xuFeEpLcKq1pr/AKt+GvIaACs9QdnyiqQASWWsM3DTEQsDgA2M0h3QU21lTh6v1f\n' +
            'L1454CoyvJ+8PcNhTGq3pLsEJOMCBfb1N/YqlZJwv0uh69fKkV/Q6LizoNKByHqj\n' +
            'HcRMxxCepSPf8PzG2SbQqwe/D9DLNz3NrPI7uA200LayukqPUfKrQcyt0SFLeZ8m\n' +
            'P1gLICfRXWY83bmBdr41yEkLdwwNVZPHb7X61wIDAQABAoIBAFguiIQezPoKnfRg\n' +
            'y9ZCDJi5i3dj149vwczTL2QmfQVUsAZv6jgbsgB75saNcHsIdUs2CJigDbhia5zU\n' +
            '3ejhAXCTWtG3HddygTyRjZENUk/9bNmcSewg443LYh0aApGTsspW1nizkWyEJ47s\n' +
            'u0QPIBz9n+Va8whZXTntk9xHLe6vMjIhjEE3FP2LU0M9CKwcVQcRkblZK40ZyCTV\n' +
            'kMNZCQFcE6xV/og1hEoUrht4ARZnzkKzMbUgsySnTIzyUSxIEnnXg0NPvAk3QqDQ\n' +
            'ZfdrzXVQN7q/uJQ9XDYQP0LxB4vp+3KLvBWfTsm+zB1xk2nzQN3TCMSLtN9Zqv3c\n' +
            'gXtFN+UCgYEA4jLeYeb/pFQFXNSOU8ZpGonb0CwZq+V03gggLID1REbGi7Op0g+b\n' +
            'oHky6TwAk8UMafw0VxV6eXhyCWTsb5DVfFHpoBaoHGD7bifZvUbIuxLhLPSkThaC\n' +
            'jBXL4urARiuHDLKuUOKqjVzKJroNwgc8XtyAlayniRHqNtYe3UKxWo0CgYEA4w8/\n' +
            'uNA9AaweAMcbcmsozNisUpf9WarVW9QpIKCyzgP+A4QVSM8IXGrTmbFUqJNN3HHd\n' +
            'l+dm82ZqWeZmOp24xaymVm+m10zM92zv7Z2/xc3nAqCrmLSq4X/3MFPPmUux6uCY\n' +
            'sY1nFnU84gjDui9+F5hTkAjkeZ5SJmpqkkra4/MCgYBYVjlWzpmjmVSGzlPPD7tY\n' +
            'K+J2R+K2qOOdGltGRg6C3YkIYd++M245m1twjohDLT8N3Pqrz62G5qzSUmGrqEpX\n' +
            'RL4SUQbSHmXSvSlhq9UUiej6X4gTHuNYxeJb+gWg4braEKXW3M5yanxf52UcEytW\n' +
            'eJKW4ViERUTMqY5gJn2QuQKBgCOaLH+qBybP+hRdI+yY6NjV0I9SzaLVqoaviZNY\n' +
            'HzE2ZwneGAhGjiUPiwY6S+/yyO6b3AcJfZcEpABcpALk4Cf+nkZHU33GyQyyxBwZ\n' +
            '9Avr8SD0WHOlQHYDPpsRSSfcOpV9ifOtHFDlKRq4DRiQgdDpTZD0E/8N2Au1++fc\n' +
            'XN4JAoGBAI7TAfs9DFGoDojBd5+vkiyRl1/yddezeCg7uzqtNyeI6I9m73E25W5R\n' +
            'gtiTj65phHaEdVZl0GES4PP21OAVLtOe5GnwcQGfYTTbEY+ZU9ftwVhTMF2+IcBS\n' +
            '5yh6KJp3dsE1cul8zpnRzF2QUhhaFxOjPplDitxWjCpvy/PIcdGT'
        //使用公钥加密
        var encrypt = new JSEncrypt();
		  //encrypt.setPrivateKey('-----BEGIN RSA PRIVATE KEY-----'+PRIVATE_KEY+'-----END RSA PRIVATE KEY-----');
        encrypt.setPublicKey('-----BEGIN PUBLIC KEY-----' + PUBLIC_KEY + '-----END PUBLIC KEY-----');
        var encrypted = encrypt.encrypt('123')
        console.log('加密后数据:%o', encrypted);

        //使用私钥解密
        var decrypt = new JSEncrypt();
		//decrypt.setPublicKey('-----BEGIN PUBLIC KEY-----' + PUBLIC_KEY + '-----END PUBLIC KEY-----');
        decrypt.setPrivateKey('-----BEGIN RSA PRIVATE KEY-----'+PRIVATE_KEY+'-----END RSA PRIVATE KEY-----');
        var uncrypted = decrypt.decrypt(encrypted);
        console.log('解密后数据:%o', uncrypted);
    </script>

</html>